package 数据结构.avl;

public class AVLTreeDemo {
    public static void main(String[] args) {
        //  int[] arr = {4, 3, 6, 4, 7, 8};
        //int[] arr = {10, 12, 8, 9, 7, 6};
        int[] arr = {10, 7, 11, 7, 8, 9};
        AVLTree avlTree = new AVLTree();
        //添加节点
        for (int i = 0; i < arr.length; i++) {
            avlTree.add(new Node(arr[i]));
        }
        System.out.println("中序遍历");
        avlTree.infixOrder();
        System.out.println("树高度" + avlTree.getRoot().height());
        System.out.println("左子树" + avlTree.getRoot().leftHeigh());
        System.out.println("右子树" + avlTree.getRoot().rightHeigh());


    }
}


//创建二叉排序树
class AVLTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }


    //中序
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("当前数组为空");
        }
    }

    //查找删除节点
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //查找删除节点的父节点
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    /**
     * 删除一node为根节点的二叉排序树的最小节点的值
     *
     * @param node 传入的节点
     * @return 以node为根节点的二叉排序数的最小节点值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        //这是target就指向了最小的节点
        deleteNode(target.value);
        return target.value;
    }

    //删除节点
    public void deleteNode(int value) {
        if (root == null) {
            return;
        } else {
            Node targetNode = search(value);
            if (targetNode == null) {//没有该节点
                return;
            }
            if (root.right == null && root.left == null) {
                root = null;
                return;
            }
            //去找到targetnode的父节点
            Node parent = searchParent(value);
            //若果要删除的节点,是叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断target是父节点的左子节点还是右子节点
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {
                //删除两颗子树的节点
                int minVal = delRightTreeMin(targetNode.right);
                targetNode.value = minVal;

            } else {
                //删除1课子树
                if (parent != null) {
                    if (targetNode.left != null) {
                        if (targetNode.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }

                } else {
                    if (parent != null) {
                        if (parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else {
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }
        }

    }
}

class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    //返回左子树高度
    public int leftHeigh() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    //右子树高度
    public int rightHeigh() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }

    //返回当前节点的高度,以该节点为根节点的树的高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //左旋转的
    private void leftRotate() {

        Node newNode = new Node(value);
        //把他设置为当前节点的左子树
        newNode.left = left;
        //把新节点的右子树,设置为当前节点右子树的左子树
        newNode.left = right.left;
        //把当前节点的值换成右子节点的值
        value = right.value;
        //吧当前节点的右子树,设置成当前节点的右子树的右子树
        right = right.right;
        left = newNode;

    }


    //右旋转
    public void rightRotate() {
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.right = left.right;
        value = left.value;
        left = left.left;
        right = newNode;
    }


    //查找要删除的值
    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }

    //查找删除元素的父节点
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            //如果查找的值小于当前节点的值,并且当前节点左子节点不为空
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;//没有找到父节点
            }
        }
    }

    //添加节点的方法
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的节点的值,和当前子树的根节点的关系
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {//添加的节点的值大于当前节点的值
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
        //添加完一个节点后,右子树的高度-左子树的高度>1,该左旋
        if (rightHeigh() - leftHeigh() > 1) {
            if (right != null && right.leftHeigh() > right.rightHeigh()) {
                //先右旋
                right.rightRotate();
                leftRotate();
            } else {
                leftRotate();
            }
            return;
        }
        //添加完一个节点后,左子树-右子树的高度>1,该优选
        if (leftHeigh() - rightHeigh() > 1) {
            if (left != null && left.rightHeigh() > left.leftHeigh()) {
                //该先进性做左转
                left.leftRotate();
                rightRotate();
            } else {
                rightRotate();
            }
        }

    }

    //中序
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}